package org.hao.samples.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hao.samples.model.PagerModel;
import org.hao.samples.model.Student;
import org.hao.samples.model.StudentVO;
import org.hao.samples.util.SystemContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class StudentHibernateDAO extends BaseHibernateDAO<Student> implements IStudentDAO {

	@Override
	public List<Student> findAllStudent() {
		String hql = "select st FROM Student as st ,Role as ro where st.user_id = ro.userId and ro.roleId=3";
		return this.find(hql);
	}

	@Override
	public void createStudent(Student student){
		this.save(student);
	}

	@Override
	public Student getUserById(Integer id) {
		return this.get(id);
	}

	@Override
	public void editStudent(Student student) throws Exception {
		this.update(student);
	}

	@Override
	public void deleteStudent(Integer id) throws Exception {
		Student student = this.get(id);
		if (student != null) {
			this.delete(student);
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Student> getAllUserBySubject(Integer id) {
		// StringBuilder sql = new StringBuilder();
		// sql.append(" FROM Student");
		// sql.append(" from exam_user_subject eus ");
		// sql.append(" left join exam_subject es on eus.sub_id = es.sub_id ");
		// sql.append(" left join exam_user user on user.user_id = eus.user_id
		// ");
		// sql.append(" where ExamUserSubject.sub_id = : subjectId ");

		String sql = "select student from Student as student ,ExamUserSubject as US "
				+ "where US.subject.sub_id =:subjectId and student.user_id = US.student.user_id";
		return this.createQuery(sql).setInteger("subjectId", id).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<StudentVO> findAllByPage() {
		PagerModel<StudentVO> page = new PagerModel<StudentVO>();
		ArrayList<StudentVO> dates = new ArrayList<StudentVO>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();

		// 使用hibernate完成分页
		String hql = "select st FROM Student as st ,Role as ro where st.user_id = ro.userId and ro.roleId=3";
		List<Student> tempDates = this.getSession().createQuery(hql).setFirstResult(offset)
				.setMaxResults(pageSize).list();
		for (Student a : tempDates) {
			StudentVO vo = new StudentVO();
			BeanUtils.copyProperties(a, vo);

			// date转String
			if (a.getBirthday() != null) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				java.util.Date date = a.getBirthday();
				String str = sdf.format(date);
				vo.setBirthday(str);
			}
			dates.add(vo);
		}
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);

		// 获取总记录数
		String hql1 = "select  count(*) FROM Student as st ,Role as ro where st.user_id = ro.userId and ro.roleId=3";
		long totalSize = (long) this.getSession().createQuery(hql1).uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}
}
